home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / prolog / sbprolog / v3 / sim.lha / sim / builtin / saverest.c < prev    next >
C/C++ Source or Header  |  1990-04-12  |  6KB  |  203 lines

  1. /************************************************************************
  2. *                                    *
  3. * The SB-Prolog System                            *
  4. * Copyright SUNY at Stony Brook, 1986; University of Arizona, 1987    *
  5. *                                    *
  6. ************************************************************************/
  7.  
  8. /*-----------------------------------------------------------------
  9. SB-Prolog is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY.  No author or distributor
  11. accepts responsibility to anyone for the consequences of using it
  12. or for whether it serves any particular purpose or works at all,
  13. unless he says so in writing.  Refer to the SB-Prolog General Public
  14. License for full details.
  15.  
  16. Everyone is granted permission to copy, modify and redistribute
  17. SB-Prolog, but only under the conditions described in the
  18. SB-Prolog General Public License.   A copy of this license is
  19. supposed to have been given to you along with SB-Prolog so you
  20. can know your rights and responsibilities.  It should be in a
  21. file named COPYING.  Among other things, the copyright notice
  22. and this notice must be preserved on all copies. 
  23. ------------------------------------------------------------------ */
  24. /* saverest.c */
  25.  
  26. #include <sys/file.h>
  27. #include "builtin.h"
  28.  
  29. b_SAVE()    /* reg1 points to PSC entry for constant giving file name */
  30. {
  31.    register LONG     op1;
  32.    register LONG_PTR top;
  33.    PSC_REC_PTR       psc_ptr;
  34.    CHAR              fname[256];
  35.  
  36.    op1 = reg[1];  DEREF(op1);
  37.    psc_ptr = GET_STR_PSC(op1);
  38.    namestring(psc_ptr, fname);
  39.    if (save(fname) < 0) {
  40.       printf("Cannot save to file '%s'\n",fname);
  41.       FAIL0;
  42.    }
  43. }
  44.  
  45.  
  46. save(filename)
  47. CHAR_PTR filename;
  48. {
  49.    int  fd, i;
  50.    LONG space;
  51.  
  52.    fd = open(filename, (O_WRONLY | O_CREAT), 0644);
  53.    if (fd < 0)
  54.       return fd;
  55.    else {
  56.       i = 19;                  /* "magic no. for saved states */
  57.       write(fd, &i, 4);
  58.       write(fd, &maxpspace, 4);
  59.       write(fd, &maxmem, 4);
  60.       write(fd, &maxtrail, 4);
  61.       write(fd, &pspace, 4);
  62.       write(fd, &memory, 4);
  63.       write(fd, &tstack, 4);
  64.       write(fd, &curr_fence, 4);
  65.       write(fd, &breg, 4);
  66.       write(fd, &ereg, 4);
  67.       write(fd, &hreg, 4);
  68.       write(fd, &trreg, 4);
  69.       write(fd, &hbreg, 4);
  70.       write(fd, &cpreg, 4);
  71.       write(fd, &pcreg, 4);
  72.       write(fd, &nil_sym, 4);
  73.       write(fd, &list_str, 4);
  74.       write(fd, &list_psc, 4);
  75.       write(fd, &comma_psc, 4);
  76.       write(fd, &interrupt_psc, 4);
  77.       write(fd, &trap_vector[0], 4);
  78.       write(fd, &trap_vector[1], 4);
  79.  
  80.       for (i = 0; i < 10; i++)
  81.          write(fd, &flags[i], 4);
  82.  
  83.       space = (LONG)curr_fence - (LONG)pspace;
  84.       i = write(fd, pspace, space);
  85.       if (i < space) {
  86.          close(fd);
  87.          return -1;
  88.       }
  89.       space = (LONG)hreg - (LONG)heap_bottom;
  90.       i = write(fd, heap_bottom, space);
  91.       if (i < space) {
  92.          close(fd);
  93.          return -1;
  94.       }
  95.       space = (LONG)local_bottom - ((LONG)ereg - 1040);
  96.       i = write(fd, ((LONG)ereg-1040), space);
  97.       if (i < space) {
  98.          close(fd);
  99.          return -1;
  100.       }
  101.       space = (LONG)trail_bottom - (LONG)trreg;
  102.       i = write(fd, trreg, space);
  103.       if (i < space) {
  104.          close(fd);
  105.          return -1;
  106.       }
  107.       close(fd);
  108.       return 1;
  109.     }
  110. }
  111.  
  112.  
  113. b_RESTORE()     /* reg1 points to PSC entry for constant giving file name */
  114. {
  115.     register LONG     op1;
  116.     register LONG_PTR top;
  117.     int               n;
  118.     PSC_REC_PTR       psc_ptr;
  119.     CHAR              fname[256];
  120.  
  121.     op1 = reg[1];  DEREF(op1);
  122.     psc_ptr = GET_STR_PSC(op1);
  123.     namestring(psc_ptr, fname);
  124.     n = restore(fname);
  125.     if (n == -1) {
  126.        char errormsg[128];
  127.        sprintf(errormsg, "Cannot restore file '%s'\n",fname);
  128.        quit(errormsg);
  129.     }
  130.     else if (n == -2)
  131.        quit(
  132.          "Saved state parameters do not match current values\n");
  133. }
  134.  
  135.  
  136. restore(fname)
  137. CHAR_PTR fname;
  138. {
  139.    int      fd, i;
  140.    LONG     space;
  141.    LONG_PTR npspace, nmemory, ntrail;
  142.  
  143.    fd = open(fname, O_RDONLY, 0644);
  144.    if (fd < 0)
  145.       return fd;
  146.    else {
  147.       read(fd, &i, 4);
  148.       if (i != 19) {
  149.          printf("restore: file '%s' is not an SB-Prolog saved state\n",fname);
  150.          return -1;
  151.       }
  152.       read(fd, &maxpspace, 4);
  153.       read(fd, &maxmem, 4);
  154.       read(fd, &maxtrail, 4);
  155.       read(fd, &npspace, 4);
  156.       read(fd, &nmemory, 4);
  157.       read(fd, &ntrail, 4);
  158.       read(fd, &curr_fence, 4);
  159.       read(fd, &breg, 4);
  160.       read(fd, &ereg, 4);
  161.       read(fd, &hreg, 4);
  162.       read(fd, &trreg, 4);
  163.       read(fd, &hbreg, 4);
  164.       read(fd, &cpreg, 4);
  165.       read(fd, &pcreg, 4);
  166.       read(fd, &nil_sym, 4);
  167.       read(fd, &list_str, 4);
  168.       read(fd, &list_psc, 4);
  169.       read(fd, &comma_psc, 4);
  170.       read(fd, &interrupt_psc, 4);
  171.       read(fd, &trap_vector[0], 4);
  172.       read(fd, &trap_vector[1], 4);
  173.  
  174.       for (i = 0; i < 10; i++)
  175.          read(fd, &flags[i], 4);
  176.  
  177.       if (pspace != npspace)
  178.      return -2;
  179.       if (memory != nmemory)
  180.      return -2;
  181.       if (tstack != ntrail)
  182.      return -2;
  183.       space = (LONG)curr_fence - (LONG)pspace;
  184.       i = read(fd, pspace, space);
  185.       if (i != space)
  186.      return -1;
  187.       space = (LONG)hreg - (LONG)heap_bottom;
  188.       i = read(fd, heap_bottom, space);
  189.       if (i != space)
  190.      return -1;
  191.       space = (LONG)local_bottom - ((LONG)ereg-1040); /* AR has at most 256
  192.                                                          pvars, + misc info */
  193.       i = read(fd, ((LONG)ereg-1040), space);
  194.       if (i != space)
  195.      return -1;
  196.       space = (LONG)trail_bottom - (LONG)trreg;
  197.       i = read(fd, trreg, space);
  198.       if (i != space)
  199.      return -1;
  200.       return 1;
  201.     }
  202. }
  203.